﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WpfSnqkGasAnalysis.IData;
using WpfSnqkGasAnalysis.Model;

namespace WpfSnqkGasAnalysis.IData_impl
{
    /// <summary>
    /// 操作日志。实现数据接口
    /// </summary>
    public sealed class Operate_logDAL_impl : BaseDAL, IOperate_logDAL
    {
        public void AddOperateLog(Login_user user)
        {
            var task = new Task((a) =>
               {
                   var user = a as Login_user;
                   Snowflake snowflake = new Snowflake(1, 1);
                   long id = snowflake.NextId();

                   string sql = @"INSERT INTO operate_log (
                            id,
                            user_real_name,
                            user_id,
                            operate_time,
                            operate_remark,
                            operate_type,
                            target_key,
                            operate_before_data,
                            operate_after_data
                        )
                        VALUES (
                            $id,
                            $user_real_name,
                            $user_id,
                            $operate_time,
                            $operate_remark,
                            $operate_type,
                            $target_key,
                            $operate_before_data,
                            $operate_after_data
                        );";
                   Dictionary<string, object> parames = new Dictionary<string, object>();
                   parames.Add("$id", id);
                   parames.Add("$user_real_name", user.User_real_name);
                   parames.Add("$user_id", user.Id);
                   parames.Add("$operate_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"));
                   parames.Add("$operate_remark", user.E3);
                   parames.Add("$operate_type", user.E12);

                   //新增时间：2022-12-12 10:15:20
                   parames.Add("$target_key", user.E1);
                   parames.Add("$operate_before_data", user.E4);
                   parames.Add("$operate_after_data", user.E5);

                   int count = base.ExecSqlCmd(sql, parames, cmd =>
                 {
                     return cmd.ExecuteNonQuery();
                 });
                   if (count > 0)
                   {
                       LogHelpter.AddLog("操作日志记录成功，用户id=" + user.Id, null, "operate_log");
                   }
               }, user);
            task.Start();
        }

        public Operate_log GetOperateLog(long id)
        {
            Operate_log operate_Log = null;
            string sql = "select  *  from  operate_log  where  id=" + id;
            base.ExecSqlCmd(sql, null, cmd =>
         {
             var reader = cmd.ExecuteReader();
             if (reader.HasRows)
             {
                 operate_Log = new Operate_log();
                 operate_Log.Id = reader.GetInt64(0);
                 operate_Log.User_real_name = Convert.ToString(reader["User_real_name"]);
                 operate_Log.User_id = reader["User_id"] == DBNull.Value ? (long)0 : Convert.ToInt64(reader["User_id"]);
                 operate_Log.Operate_time = Convert.ToString(reader["Operate_time"]);
                 operate_Log.Operate_remark = Convert.ToString(reader["Operate_remark"]);
                 operate_Log.Operate_type = reader["Operate_type"] == DBNull.Value ? (short)0 : Convert.ToInt16(reader["Operate_type"]);
                 operate_Log.Target_key = (long)reader["Target_key"].ToString().ToMyDecimal();
                 operate_Log.Operate_before_data = Convert.ToString(reader["Operate_before_data"]);
                 operate_Log.Operate_after_data = Convert.ToString(reader["Operate_after_data"]);
             }
             else
             {
                 operate_Log = null;
             }
             reader.Close();
             reader.DisposeAsync();
         });
            return operate_Log;
        }

        public PageModel<Operate_log> PageOperateLogAsync(Operate_log log, int p, int rows)
        {
            PageModel<Operate_log> pageModel = new PageModel<Operate_log>();

            StringBuilder sb = new StringBuilder();
            sb.AppendLine(" select  *  from  operate_log  where  1=1  ");

            StringBuilder parameSb = new StringBuilder();
            Dictionary<string, object> parames = new Dictionary<string, object>();
            if (log != null)
            {
                if (!string.IsNullOrWhiteSpace(log.User_real_name))
                {
                    parameSb.AppendLine("  and  user_real_name  like  $realName  ");
                    parames.Add("$realName", "%" + log.User_real_name + "%");
                }
                if (!string.IsNullOrWhiteSpace(log.E3))
                {
                    parameSb.AppendLine("  and  operate_time>=$dateStart  ");
                    parames.Add("$dateStart", log.E3);
                }
                if (!string.IsNullOrWhiteSpace(log.E4))
                {
                    parameSb.AppendLine("  and  operate_time<=$dateEnd  ");
                    parames.Add("$dateEnd", log.E4);
                }
            }
            sb.Append(parameSb.ToString());

            //查询总记录数
            string sqlTotal = "select count(id)  from  operate_log  " + parameSb.ToString();
            pageModel.Total = base.ExecSqlCmd(sqlTotal, parames, cmd =>
           {
               var b = cmd.ExecuteScalar();
               if (b != null)
               {
                   return Convert.ToInt32(b);
               }
               return 0;
           });

            //查询当前页数据
            List<Operate_log> list = new List<Operate_log>();
            base.ExecSqlCmd(sb.ToString(), parames, cmd =>
        {
            var reader = cmd.ExecuteReader();
            while (reader.Read())
            {
                Operate_log operate_Log = new Operate_log();
                operate_Log.Id = reader.GetInt64(0);
                operate_Log.User_real_name = Convert.ToString(reader["User_real_name"]);
                operate_Log.User_id = reader["User_id"] == DBNull.Value ? (long)0 : Convert.ToInt64(reader["User_id"]);
                operate_Log.Operate_time = Convert.ToString(reader["Operate_time"]);
                operate_Log.Operate_remark = Convert.ToString(reader["Operate_remark"]);
                operate_Log.Operate_type = reader["Operate_type"] == DBNull.Value ? (short)0 : Convert.ToInt16(reader["Operate_type"]);
                list.Add(operate_Log);
            }
            reader.Close();
        });
            pageModel.PageIndex = p;
            pageModel.PageSize = rows;
            return pageModel;
        }
    }
}
